<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\NewsColumnTypeModel;
use Common\Model\NewsRewardModel;

class NewsController extends AdminbaseController
{
	private $video_type    = 4;
	private $vr_video_type = 5;
	private $text          = 6;
	private $image_text    = 7;
	
    function _initialize()
    {
        parent::_initialize();
    }

    function index()
    {
        $column_id = I("column_id") ? I("column_id") : '';
        $area_id   = I("area_id") ? I("area_id") : '';
        $id_type = '';
        $id_val  = '';

        $admin_id = $_SESSION['ADMIN_ID']; //当前用户ID
        $live_open = 0; //直播权限状态 0关闭，1开启
        $live_role = 0; //直播权限 0无，1有 
        if($column_id){
            //查看栏目 直播权限状态 开启/关闭
            if($admin_id == 1){
                $live_open = 1;
                $live_role = 1;
            }else {
                $live_sel = M("vrnews1.news_column")->where('column_id = '.$column_id)->field("live_status")->select();
                $live_open = $live_sel[0]['live_status'];
                //查看 当前管理员 是否有直播权限
                $live_admin = M("vrnews1.column_live_admin")->where("column_id = " . $column_id . " and admin_id = ".$admin_id." and type = 0")->select();
                if ($live_admin) {
                    $live_role = 1;
                }
            }

            $id_type = "column_id";
            $id_val  = $column_id;
        }else{
            //查看栏目 直播权限状态 开启/关闭
            if($admin_id == 1){
                $live_open = 1;
                $live_role = 1;
            }else {
                $live_sel = M("vrnews1.area")->where('area_id = ' . $area_id)->field("live_status")->select();
                $live_open = $live_sel[0]['live_status'];
                //查看 当前管理员 是否有直播权限
                $live_admin = M("vrnews1.column_live_admin")->where("column_id = " . $area_id . " and admin_id = ".$admin_id." and type = 1")->select();
                if ($live_admin) {
                    $live_role = 1;
                }
            }
            
            $id_type = "area_id";
            $id_val  = $area_id;
        }
        if(!$id_type || !$id_val){
            $this->error("请先选择 栏目！");
        }
//        $category = $this->getCategory();
//        $this->assign("category", $category);
        $formParam['newsTitle'] = I('param.newsTitle') ? I('param.newsTitle') : "";
        $and_newstitle = '';
        $kros=I('param.newsTitle');
        if(!empty($kros)){
            $and_newstitle = " and title like '%".I('param.newsTitle')."%'";
            $formParam['newsTitle'] = I('param.newsTitle');
        }else{
            $formParam['newsTitle'] = "";
        }
        
        //搜索 栏目
        $news_type = (int)I('news_type');
        if($news_type && $news_type > 1){
        	$and_type = ' AND vrnews1.news.type='.$news_type.' ';
        	if($news_type == $this->video_type){
        		//普通视频
        		$and_type = ' AND vrnews1.news.type=4 AND vrnews1.news_video.category=1';
        	}elseif($news_type == 6){
        		//VR视频
        		$and_type = ' AND vrnews1.news.type=5';
        	}
        	elseif($news_type == $this->vr_video_type){
        		//VR视频
        		$and_type = ' AND vrnews1.news.type=4 AND vrnews1.news_video.category=2';
        	}elseif($news_type == 7){
        		$and_type = " AND vrnews1.news.type=$this->text ";
        	}elseif($news_type == 8){
        		$and_type = " AND vrnews1.news.type=$this->image_text ";
        	}
        	
        }
        // 处理 搜索项内的 新闻分类
        $and_newstype = '';
        if($column_id) {
            $cates = new NewsColumnTypeModel();
            $get_cates = $cates->where("column_id = $column_id and weight >= 70 and status = 1")->field("type_id,name")->select();
            if(!I('param.category')) { //没有筛选项
                $formParam['categoryId'] = $get_cates;
                $this->assign('cate_sel', "selected");
            }else{ //选择筛选项
                $and_newstype = " and type_id = ".I('param.category');
                foreach($get_cates as $key=>$val){
                    $formParam['categoryId'][$key]['type_id'] = $val['type_id'];
                    $formParam['categoryId'][$key]['name']    = $val['name'];
                    if($val['type_id'] == I('param.category')){
                        $formParam['categoryId'][$key]['selected'] = "selected";
                    }else{
                        $formParam['categoryId'][$key]['selected'] = '';
                    }
                }
            }
        }else{
            $formParam['categoryId'] = '';
        }


        $this->assign("formParam", $formParam);

        $currentPage = I('param.p', 0);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }

        $pageSize = '10';
        $pageStart = ($currentPage - 1) * $pageSize;
        $pageEnd = $currentPage * $pageSize;
        $news = M("vrnews1.news", NULL);
        
        /* //轮播列表
        if($currentPage == '1'){
        	$carousel_figure_list = $news->where("$id_type = $id_val and special_news_id = 0 and  vrnews1.news.type =2" .$and_newstitle .$and_newstype. $and_type)
	            ->join("vradmin1.admin_user on admin_user.admin_id = vrnews1.news.creator_id", 'left')
	            ->join("vrnews1.news_video on vrnews1.news_video.news_id = vrnews1.news.news_id", 'left')
	            ->field("vrnews1.news.*, vradmin1.admin_user.username,year(vrnews1.news.create_time) as year1,month(vrnews1.news.create_time) as month1,day(vrnews1.news.create_time) as day1,year(from_unixtime(refresh_time)) as year,month(from_unixtime(refresh_time)) as month,day(from_unixtime(vrnews1.news.refresh_time)) as day,from_unixtime(refresh_time) as refresh_time")
	            ->Order("
            		case  when year is null then year1 else year end desc,
            		case  when month is null then month1 else month end desc,
            		case  when day is null then day1 else day end desc,
            		weight desc,
            		refresh_time desc,
            		create_time desc")->select();
        }else{
        	$carousel_figure_list = '';
        } */
   
        $list = $news->where("$id_type = $id_val and special_news_id = 0 and vrnews1.news.status=0 and  vrnews1.news.type !=2" .$and_newstitle .$and_newstype. $and_type)
            ->join("vradmin1.admin_user on admin_user.admin_id = vrnews1.news.creator_id", 'left')
            ->join("vrnews1.news_video on news.news_id = news_video.news_id",'left')
            ->field("vrnews1.news.*,vrnews1.news_video.status as video_status, vrnews1.news_video.category as video_category, vradmin1.admin_user.username,year(vrnews1.news.create_time) as year1,month(vrnews1.news.create_time) as month1,day(vrnews1.news.create_time) as day1,year(from_unixtime(refresh_time)) as year,month(from_unixtime(refresh_time)) as month,day(from_unixtime(refresh_time)) as day,from_unixtime(refresh_time) as refresh_time")
            ->Order("
            		top_status desc,
            		case  when year is null then year1 else year end desc,
            		case  when month is null then month1 else month end desc,
            		case  when day is null then day1 else day end desc,
            		weight desc,
            		refresh_time desc,
            		create_time desc")
            ->limit($pageStart,$pageSize)->select();
        $count = $news->where("$id_type = $id_val and special_news_id = 0 and vrnews1.news.status=0 and  vrnews1.news.type !=2" .$and_newstitle .$and_newstype. $and_type)
            ->join("vradmin1.admin_user on admin_user.admin_id = vrnews1.news.creator_id", 'left')
            ->join("vrnews1.news_video on vrnews1.news_video.news_id = vrnews1.news.news_id", 'left')
            ->count();
        if(!empty($carousel_figure_list)){
        	foreach($carousel_figure_list as $key=>&$val){
        		$str = '';
        		if($val['web_pub'] == 1){
        			$str .= '门户网站, ';
        		}
        		if($val['app_pub'] == 1){
        			$str .= 'APP';
        		}
        		$carousel_figure_list[$key]['pub'] = rtrim($str, ',');
        		
        		$val['type_zh'] = $this->_getNewType($val);
        		
        		/*打赏汇闻币*/
        		$carousel_figure_list[$key]['hw_money_total'] = '0';
        		$news_reward = new NewsRewardModel();
        		$news_reward_list = $news_reward->where("news_id = ".$val['news_id'])->field("hw_money")->select();
        		foreach($news_reward_list as $reward_key=>$reward_value){
        			$carousel_figure_list[$key]['hw_money_total'] +=$reward_value['hw_money'];//number_format($reward_value['hw_money'],'2');
        		}
        		
        	}
        	unset($val);
        }

        if(!empty($list)){
            foreach($list as $key=>&$val){
                $str = '';
                if($val['web_pub'] == 1){
                    $str .= '门户网站, ';
                }
                if($val['app_pub'] == 1){
                    $str .= 'APP';
                }
                $list[$key]['pub'] = rtrim($str, ',');

                /* 显示类型 */
                $val['type_zh'] = $this->_getNewType($val);
                
                /* 如果是引用的视频显示转码成功 */
                if($val['reference_id'] && $val['reference_type'] == 1){
                	$val['video_status'] = 2;
                }
                
                /*打赏汇闻币*/
                $list[$key]['hw_money_total'] = '0';
                $news_reward = new NewsRewardModel();
                $news_reward_list = $news_reward->where("news_id = ".$val['news_id'])->field("hw_money")->select();
                foreach($news_reward_list as $reward_key=>$reward_value){
                	$list[$key]['hw_money_total'] +=$reward_value['hw_money'];//number_format($reward_value['hw_money'],'2');
                }
            }
            unset($val);
        }

        $this->assign("carousel_figure_data", $carousel_figure_list);
        $this->assign("data", $list);
        $page = $this->page($count ,10);
        $this->assign("page", $page->show('Admin'));
        $this->assign("id_type",$id_type);
        $this->assign("id_val",$id_val);
//        $this->assign("column_id", $column_id);
		//1 快捷发布 2 轮播图 3 专题 4 视频 5 图集 6文本 7图文
        $type_list = [
        		['id'=>1, 'name'=>'全部'],
        		//['id'=>2, 'name'=>'轮播图 '],
        		['id'=>3, 'name'=>'专题'],
        		['id'=>4, 'name'=>'普通视频'],
        		['id'=>5, 'name'=>'VR视频'],
        		['id'=>6, 'name'=>'图集'],
        		['id'=>7, 'name'=>'文本'],
        		['id'=>8, 'name'=>'图文'],
                ['id'=>9, 'name'=>'视频直播'],
                ['id'=>10, 'name'=>'VR直播'],
                ['id'=>11, 'name'=>'图文直播'],
                ['id'=>12, 'name'=>'视频加图文直播'],
                ['id'=>13, 'name'=>'VR加图文直播'],
                ['id'=>14, 'name'=>'录播']
        ];
		$this->assign('type_list', $type_list);
		$this->assign('select_type', $news_type);
        $this->assign('live_open',$live_open);
        $this->assign('live_role',$live_role);
        $this->display();
    } 

    function news_reward_list(){
    	$news_id = I("news_id") ? I("news_id") : '';
    	$this->assign('news_id',$news_id);
    	$this->display('news_reward_lists');
    }

    

    #region 删除新闻
    function del()
    {
        $newsId = I('param.newsId', "");
        if (empty($newsId)) {
            $this->error("参数错误！");
        }
        $result = $this->curl_http(C('news_url') . 'news/delNews?newsIds=' . $newsId);
        if ($result['Success']) {
            $this->success("修改成功！", U("Admin/News/index"));
        } else {
            return $this->error($result['Message']);
        }
    }
    #endregion

    #region 普通新闻
    function NewsAdd()
    {
        $this->assign("Title", "新闻");
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('newsadd');
    }

    function NewsEdit($newsId)
    {
        $this->assign("Title", "新闻");
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $this->assign("news", $news);

        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('newsedit');
    }

    function NewsPost()
    {
        $newsId = I("param.newsId", '');
        $newsType = 1;
        $newsKey = I("param.newsKey", '');
        $newsTitle = I("param.newsTitle", '');
        $newsVeneto = I("param.newsVeneto", '');
        $newsSource = I("param.newsSource", '');
        $imageUrl = I("param.imageUrl", '');
        $province = I("param.province","");
        $city = I("param.city", "");
        $category = I("param.category", "");
        $tags = I("param.tags", "");
        if (empty($newsKey)) {
            $this->error("请填写关键字！");
        }
        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsVeneto)) {
            $this->error("请填写新闻摘要！");
        }
        if (empty($newsSource)) {
            $this->error("请填写新闻来源！");
        }
        if (empty($imageUrl)) {
            $this->error("请上传新闻图片！");
        } else {
            if (strpos($imageUrl, "http://") === false) {
                $imageUrl = "http://{$_SERVER['HTTP_HOST']}" . $imageUrl;
            }
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if (empty($category)) {
            $this->error("请选择分类！");
        }
        $newsContent = $_POST["newsContent"];
        if (empty($newsContent)) {
            $this->error("新闻正文不能为空！");
        }
        $fields = '&newsType=' . $newsType . '&categoryId=' . $category . '&subjectTagId=' . $tags . '&titleImg=' . $imageUrl . '&keyWords=' . $newsKey . '&summary=' . $newsVeneto . '&areaId=' . $city . '&source=' . $newsSource . "&title=" . $newsTitle . "&newsContent=" . $newsContent;
        if (!empty($newsId)) {
            $successInfo = "修改成功";
            $fields = $fields . "&newsId=" . $newsId . "&modifier=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/modifyNews?token=' . $_SESSION["TOKEN"], $fields);
        } else {
            $successInfo = "添加成功";
            $fields = $fields . "&creator=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        }
        if ($result['Success']) {
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }

    function NewsDetail($newsId)
    {
        $this->assign("Title", "新闻");
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $news['newsTag'] =  $this->getTagsValue($news['categoryId'], $news['subjectTagId']);
        $this->assign("news", $news);
        $this->display('newsdetail');
    }
    #endregion

    #region 专题新闻
    function SubNewsAdd()
    {
        $this->assign("Title", "专题新闻");
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $subject = $this->getSubject();
        $this->assign("subject", $subject);
        $this->display('subnewsadd');
    }

    function SubNewsEdit($newsId)
    {
        $this->assign("Title", "专题新闻");
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $this->assign("news", $news);

        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $subject = $this->getSubject();
        $this->assign("subject", $subject);

        $this->display('subnewsedit');
    }

    function SubNewsPost()
    {
        $newsId = I("param.newsId", '');
        $newsType = 3;
        $newsKey = I("param.newsKey", '');
        $newsTitle = I("param.newsTitle", '');
        $newsVeneto = I("param.newsVeneto", '');
        $newsSource = I("param.newsSource", '');
        $imageUrl = I("param.imageUrl", '');
        $subject = I("param.subject", '');
        $province = I("param.province", "");
        $city = I("param.city", "");
        $category = I("param.category", "");
        $tags = I("param.tags", "");
        if (empty($newsKey)) {
            $this->error("请填写关键字！");
        }
        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsVeneto)) {
            $this->error("请填写新闻摘要！");
        }
        if (empty($newsSource)) {
            $this->error("请填写新闻来源！");
        }
        if (empty($imageUrl)) {
            $this->error("请上传新闻图片！");
        } else {
            if (strpos($imageUrl, "http://") === false) {
                $imageUrl = "http://{$_SERVER['HTTP_HOST']}" . $imageUrl;
            }
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if (empty($category)) {
            $this->error("请选择分类！");
        }
        $newsContent = $_POST["newsContent"];
        if (empty($newsContent)) {
            $this->error("新闻正文不能为空！");
        }
        $fields = '&newsType=' . $newsType . '&categoryId=' . $category . '&subjectTagId=' . $tags . '&titleImg=' . $imageUrl . '&keyWords=' . $newsKey . '&summary=' . $newsVeneto . '&areaId=' . $city . '&source=' . $newsSource . "&title=" . $newsTitle . "&newsContent=" . $newsContent . "&parentId=" . $subject;
        if (!empty($newsId)) {
            $successInfo = "修改成功";
            $fields = $fields . "&newsId=" . $newsId . "&modifier=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/modifyNews?token=' . $_SESSION["TOKEN"], $fields);
        } else {
            $successInfo = "添加成功";
            $fields = $fields . "&creator=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        }
        if ($result['Success']) {
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }

    function SubNewsDetail($newsId)
    {
        $this->assign("Title", "新闻");
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $this->assign("news", $news);
        $news['newsTag'] =  $this->getTagsValue($news['categoryId'], $news['subjectTagId']);
        /*print_r($news);
        die();*/
        $this->display('subnewsdetail');
    }

    #endregion

    #region 图片新闻
    function PictureAdd()
    {
//        $this->assign("Title", "图片新闻");
//        $province = $this->getProvince();
//        $this->assign("province", $province);
//        $category = $this->getCategory();
//        $this->assign("category", $category);
        $this->display('pictureadd');
    }

    function PictureEdit($newsId)
    {
        $this->assign("Title", "图片新闻");
        $data = $this->curl_http(C('news_url') . "newsPic/getNewsPicList?curPage=1&pageSize=1000&newsId={$newsId}");
        $imgData = $data['ReturnData']['data'];
        $this->assign("imgData", $imgData);

        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $this->assign("news", $news);
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('pictureedit');
    }

    function PictureDetail($newsId)
    {
        $this->assign("Title", "图片新闻");
        $data = $this->curl_http(C('news_url') . "newsPic/getNewsPicList?curPage=1&pageSize=1000&newsId={$newsId}");
        $imgData = $data['ReturnData']['data'];
        $this->assign("imgData", $imgData);

        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $news['newsTag'] =  $this->getTagsValue($news['categoryId'], $news['subjectTagId']);
        $this->assign("news", $news);


        $this->display('picturedetail');
    }

    function PicturePost()
    {
        $newsType = 2;
        $newsKey = I("param.newsKey", '');
        $newsTitle = I("param.newsTitle", '');
        $newsSource = I("param.newsSource", '');
        $province = I("param.province", "");
        $city = I("param.city", "");
        $category = I("param.category", "");
        $tags = I("param.tags", "");

        $pictureTag = $_POST['pictureTag'];
        $imageUrl = $_POST['imageUrl'];
        $pictureDesc = $_POST['pictureDesc'];
        $pictureCount = I("param.pictureCount", 0);

        if (empty($newsKey)) {
            $this->error("请填写关键字！");
        }
        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsSource)) {
            $this->error("请填写新闻来源！");
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if ($pictureCount <= 0) {
            $this->error("请上传图片！");
        }
        if (count($pictureTag) - 1 != $pictureCount) {
            $this->error("请全部填写图片标签！");
        }
        if (count($imageUrl) - 1 != $pictureCount) {
            $this->error("您有未上传的图片！");
        } else {
            foreach ($imageUrl as $k => $v) {
                $imageUrl[$k] = "http://{$_SERVER['HTTP_HOST']}" . $v;
            }
        }
        if (count($pictureDesc) - 1 != $pictureCount) {
            $this->error("请全部填写图片描述！");
        }
        $titleArr=array();
        $usePicNum = 3;
        if ($pictureCount< $usePicNum) {
            $usePicNum = $pictureCount;
        }else{
            $usePicNum=3;
        }
        for ($i = 0; $i < $usePicNum; $i++) {
            $titleArr[] = $imageUrl[$i];
        }
        $fields = '&newsType=' . $newsType . '&keyWords=' . $newsKey . '&categoryId=' . $category . '&subjectTagId=' . $tags . '&areaId=' . $city . '&source=' . $newsSource . "&title=" . $newsTitle . "&newsContent=内容&titleImg=" . implode(',',$titleArr);
        $successInfo = "添加成功";
        $fields = $fields . "&creator=" . $this->getAdminName();
        $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        if ($result['Success']) {
            $newsId = $result['ReturnData']["data"]["newsId"];
            if (!empty($newsId)) {
                for ($i = 0; $i < $pictureCount; $i++) {
                    $currentTag = $pictureTag[$i];
                    $currentImg = $imageUrl[$i];
                    $currentDesc = $pictureDesc[$i];
                    $currentField = "&newsId={$newsId}&imgDesc={$currentDesc}&title={$currentTag}&imgUrl={$currentImg}&creator=" . $this->getAdminName();
                    $this->curl_http(C('news_url') . 'newsPic/addNewsPic?token=' . $_SESSION["TOKEN"], $currentField);
                }
            }
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }
    #endregion

    #region 视频新闻

    function VideoAdd($newsType)
    {
        if (!in_array($newsType, array(4, 5, 6))) {
            $this->error("类型错误！");
        }
        $this->assign("newsType", $newsType);
        //4视频新闻；5直播新闻；6推广
        $title = "视频新闻";
        if ($newsType == 5) {
            $title = "直播新闻";
        } elseif ($newsType == 6) {
            $title = "推广新闻";
        }
        $this->assign("Title", $title);
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('videoadd');
    }

    function VideoEdit($newsType, $newsId)
    {
        if (!in_array($newsType, array(4, 5, 6))) {
            $this->error("类型错误！");
        }
        $this->assign("newsType", $newsType);
        $title = "视频新闻";
        if ($newsType == 5) {
            $title = "直播新闻";
        } elseif ($newsType == 6) {
            $title = "推广新闻";
        }
        $this->assign("Title", $title);

        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        // print_r($news);
        //die();
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $this->assign("news", $news);

        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('videoedit');
    }

    //新闻类型：1普通新闻；2图片新闻；3专题新闻；4视频新闻；5直播新闻；6推广视频；7其它;8专题
    function VideoPost()
    {
        $newsId = I("param.newsId", '');
        $newsTitle = I("param.newsTitle", '');
        $newsType = I("param.newsType", '');
        $newsKey = I("param.newsKey", '');
        $newsPath = I("param.newsPath", '');
        $newsAuthor = I("param.newsAuthor", '');
        $imageUrl = I("param.imageUrl", '');
        $province = I("param.province", "");
        $city = I("param.city", "");
        $category = I("param.category", "");
        $tags = I("param.tags", "");
        if (!in_array($newsType, array(4, 5, 6))) {
            $this->error("类型错误！");
        }
        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsKey)) {
            $this->error("请填写关键字！");
        }
        if (empty($newsPath)) {
            $this->error("请填写路径！");
        }
        if (empty($newsAuthor)) {
            $this->error("请填写作者！");
        }
        if (empty($imageUrl)) {
            $this->error("请上传新闻图片！");
        } else {
            if (strpos($imageUrl, "http://") === false) {
                $imageUrl = "http://{$_SERVER['HTTP_HOST']}" . $imageUrl;
            }
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if (empty($category)) {
            $this->error("请选择分类！");
        }
        $fields = '&newsType=' . $newsType . '&categoryId=' . $category . '&subjectTagId=' . $tags . '&titleImg=' . $imageUrl . '&keyWords=' . $newsKey . '&linkUrl=' . $newsPath . '&areaId=' . $city . '&author=' . $newsAuthor . "&newsContent=内容&title=" . $newsTitle;
        if (!empty($newsId)) {
            $successInfo = "修改成功";
            $fields = $fields . "&newsId=" . $newsId . "&modifier=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/modifyNews?token=' . $_SESSION["TOKEN"], $fields);
        } else {
            $successInfo = "添加成功";
            $fields = $fields . "&creator=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        }
        if ($result['Success']) {
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }

    function VideoDetail($newsId)
    {
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $news['newsTag'] =  $this->getTagsValue($news['categoryId'], $news['subjectTagId']);
        $this->assign("news", $news);

        $newsType = $news['newsType'];
        $title = "视频新闻";
        if ($newsType == 5) {
            $title = "直播新闻";
        } elseif ($newsType == 6) {
            $title = "推广新闻";
        }
        $this->assign("Title", $title);
        $this->display('videodetail');
    }
    #endregion

    #region 专题

    function SubjectAdd()
    {
        $this->assign("Title", "专题");
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('subjectadd');
    }

    function SubjectEdit($newsId)
    {
        $this->assign("Title", "专题");

        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $this->assign("news", $news);
        //print_r($news);die();
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('subjectedit');
    }

    function SubjectPost()
    {
        $newsId = I("param.newsId", '');
        $newsType = 8;
        $newsTitle = I("param.newsTitle", '');
        $newsAuthor = I("param.newsAuthor", '');
        $newsVeneto = I("param.newsVeneto", '');
        $imageUrl = I("param.imageUrl", '');
        $subImageUrl = I("param.subImageUrl", '');
        $province = I("param.province", "");
        $city = I("param.city", "");
        $category = I("param.category");

        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsAuthor)) {
            $this->error("请填写作者！");
        }
        if (empty($newsVeneto)) {
            $this->error("请填写新闻摘要！");
        }
        if (empty($imageUrl)) {
            $this->error("请上传标题图片！");
        } else {
            if (strpos($imageUrl, "http://") === false) {
                $imageUrl = "http://{$_SERVER['HTTP_HOST']}" . $imageUrl;
            }
        }
        if (empty($subImageUrl)) {
            $this->error("请上传专题图片！");
        } else {
            if (strpos($subImageUrl, "http://") === false) {
                $subImageUrl = "http://{$_SERVER['HTTP_HOST']}" . $subImageUrl;
            }
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if (empty($category)) {
            $this->error("请选择分类！");
        }

        $fields = '&newsType=' . $newsType . '&categoryId=' . $category . '&titleImg=' . $imageUrl . '&subjectImg=' . $subImageUrl . '&areaId=' . $city . '&author=' . $newsAuthor . "&summary=" . $newsVeneto . "&newsContent=内容&title=" . $newsTitle;
        if (!empty($newsId)) {
            $successInfo = "修改成功";
            $fields = $fields . "&newsId=" . $newsId . "&modifier=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/modifyNews?token=' . $_SESSION["TOKEN"], $fields);
        } else {
            $successInfo = "添加成功";
            $fields = $fields . "&creator=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        }
        if ($result['Success']) {
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }

    function SubjectDetail($newsId)
    {
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $this->assign("news", $news);

        $this->assign("Title", '专题');
        $this->display('subjectdetail');
    }

    /*
     * 草稿列表
     * */
    function caogao(){
        $column_id = I("param.column_id", '');
        $area_id   = I("param.area_id", '');
        if($column_id){
            $column_typeval = '0';
            $id_val = $column_id;
            
            $id_type = "column_id";
        }else{
            $column_typeval = '1';
            $id_val = $area_id;

            $id_type = "area_id";
        }

        $this->assign("column_typeval",$column_typeval);
        $this->assign("id_type",$id_type);
        $this->assign("id_val",$id_val);
        $this->display();
    }

    /*
   * 定时稿件 列表
   * */
    function timer(){
        $column_id = I("param.column_id", '');
        $area_id   = I("param.area_id", '');
        if($column_id){
            $column_typeval = '0';
            $id_val = $column_id;
            $id_type = "column_id";
        }else{
            $column_typeval = '1';
            $id_val = $area_id;
            $id_type = "area_id";
        }

        $this->assign("column_typeval",$column_typeval);
        $this->assign("id_type",$id_type);
        $this->assign("id_val",$id_val);
        $this->display();
    }

    /*
     * 编辑页面
     * */
    function editapi(){
        $news_id    = I("param.news_id", '');
        $column_id  = I("param.column_id", '');
        $area_id    = I("param.area_id", '');
        $special_id = I("param.special_id", '');
        if($column_id){
            $column_typeval = '0';
            $id_val = $column_id;
            $id_type = "column_id";
        }else{
            $column_typeval = '1';
            $id_val = $area_id;
            $id_type = "area_id";
        }

        $this->assign("news_id",$news_id);
        $this->assign("column_typeval",$column_typeval);
        $this->assign("id_type",$id_type);
        $this->assign("id_val",$id_val);
        $this->assign("special_id",$special_id);
        $this->display();
    }


    #endregion

    #region 帮助方法

    function getAdminName()
    {
        return "admin";
    }

    function getProvince()
    {
        $data = $this->curl_http(C('news_url') . 'area/getAreaListA?lId=1');
        $data = $data['ReturnData']['data'];
        //print_r($data);
        //die();
        return $data;
    }

    function getProvinceId($aId)
    {
        $data = $this->curl_http(C('news_url') . 'area/getAreaListA?aId=' . $aId);
        $data = $data['ReturnData']['data'][0]['pId'];
        return $data;
    }

    function getAreaNmae($aId)
    {
        $data = $this->curl_http(C('news_url') . 'area/getAreaListA?aId=' . $aId);
        $data = $data['ReturnData']['data'][0]['aN'];
        return $data;
    }

    function getCity($pid)
    {
        $data = $this->curl_http(C('news_url') . "area/getAreaListA?lId=2&pId={$pid}");
        $data = $data['ReturnData']['data'];
        $result['status'] = 1;
        $result['data'] = $data;
        $this->ajaxReturn($result);
    }

    function getCategory()
    {
        $data = $this->curl_http(C('news_url') . 'newsCategory/getNewsCategoryList');
        $data = $data['ReturnData']['data'];
        return $data;
    }

    function getTags($cId)
    {
        $data = $this->curl_http(C('news_url') . "newsTag/getNewsTagList?categoryId={$cId}&curPage=1&pageSize=1000");
        $data = $data['ReturnData']['data'];
        $result['status'] = 1;
        $result['data'] = is_array($data) ? $data : array();
        $this->ajaxReturn($result);
    }

    function getTagsValue($cId, $subId)
    {
        $value = "";
        $data = $this->curl_http(C('news_url') . "newsTag/getNewsTagList?categoryId={$cId}&curPage=1&pageSize=1000");
        $data = $data['ReturnData']['data'];
        foreach ($data as $k => $v) {
            if ($v['tagId'] == $subId) {
                $value = $v['tagName'];
                break;
            }
        }
        return $value;
    }

    function getNews($newId)
    {
        $data = $this->curl_http(C('news_url') . "news/getNewsListA?&newsId={$newId}&&curPage=1&pageSize=10");
        $data = $data['ReturnData']['data'];
        //print_r($data[0]);die();
        return $data[0];
    }

    function getSubject()
    {
        $data = $this->curl_http(C('news_url') . "news/getNewsListA?&newsType=8&&curPage=1&pageSize=10");
        $data = $data['ReturnData']['data'];
        //print_r($data);die();
        return $data;
    }

    function getNewsType($newsType)
    {
        $returnArr = array();
        //1普通新闻；2图片新闻；3专题新闻；4视频新闻；5直播新闻；6推广视频；7其它;8专题
        switch ($newsType) {
            case 1:
                $returnArr = array("title" => "新闻", "edit_url" => "NewsEdit", "detail_url" => "NewsDetail");
                break;
            case 2:
                $returnArr = array("title" => "图片新闻", "edit_url" => "PictureEdit", "detail_url" => "PictureDetail");
                break;
            case 3:
                $returnArr = array("title" => "专题新闻", "edit_url" => "SubNewsEdit", "detail_url" => "SubNewsDetail");
                break;
            case 4:
                $returnArr = array("title" => "视频新闻", "edit_url" => "VideoEdit", "detail_url" => "VideoDetail");
                break;
            case 5:
                $returnArr = array("title" => "直播新闻", "edit_url" => "VideoEdit", "detail_url" => "VideoDetail");
                break;
            case 6:
                $returnArr = array("title" => "推广新闻", "edit_url" => "VideoEdit", "detail_url" => "VideoDetail");
                break;
            case 8:
                $returnArr = array("title" => "专题", "edit_url" => "SubjectEdit", "detail_url" => "SubjectDetail");
                break;
        }
        return $returnArr;
    }
    
}